package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.manger.dto.ActivityFindDto;
import com.zhentao.model.common.manger.pojo.Activity;
import com.zhentao.service.ActivityService;
import com.zhentao.mapper.ActivityMapper;
import com.zhentao.utils.common.TextModerationPlusDemo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;

/**
* @author lenovo
* @description 针对表【activity】的数据库操作Service实现
* @createDate 2024-10-02 22:46:34
*/
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
    implements ActivityService{
    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Override
    public ResponseResult findAll(ActivityFindDto dto) {
        Page<Activity> page = new Page<>(dto.getPage(),dto.getSize());
        QueryWrapper<Activity> wrapper=new QueryWrapper<>();
        wrapper.lambda().like(StringUtils.isNotBlank(dto.getName()), Activity::getActiveName, dto.getName());
        wrapper.lambda().eq(StringUtils.isNotBlank(dto.getStatus()), Activity::getStatus, dto.getStatus());
        Page<Activity> activityPage = activityMapper.selectPage(page, wrapper);
        return ResponseResult.okResult(activityPage);
    }

    @Override
    public ResponseResult add(Activity activity) throws Exception {
            activityMapper.insert(activity);
            //活动开始时间
            long startTime = activity.getStartTime().getTime();
            //现在的时间
            long currentTimeMillis = System.currentTimeMillis();
            //未来五分钟的时间
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.MINUTE, 5);
            long timeInMillis = instance.getTimeInMillis();
            //结束时间
            long endTime = activity.getEndTime().getTime();
            if(endTime>=currentTimeMillis){
                redisTemplate.opsForZSet().add("endTime",activity, endTime);
            } else if (endTime>=timeInMillis) {
                kafkaTemplate.send("endTime", JSON.toJSONString(activity));
            }
            if(startTime<=currentTimeMillis){
                kafkaTemplate.send("active", JSON.toJSONString(activity));
            }else if(startTime<=timeInMillis){
                redisTemplate.opsForZSet().add("active",activity, startTime);
            }
            return ResponseResult.okResult("添加成功");
    }
    @Scheduled(cron = "0/5 * * * * ?")
    public void checkActive(){
        //获取当前时间+0分钟
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MINUTE, 0);
        long timeInMillis = instance.getTimeInMillis();
        Set<Object> active = redisTemplate.opsForZSet().rangeByScore("active", 0, timeInMillis);
        for (Object o : active) {
            kafkaTemplate.send("active", JSON.toJSONString(o));
            redisTemplate.opsForZSet().remove("active",o);
        }
    }
    @Scheduled(cron = "0/5 * * * * ?")
    public void checkEndTime(){
        //获取当前时间+0分钟
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MINUTE, 0);
        long timeInMillis = instance.getTimeInMillis();
        Set<Object> endTime = redisTemplate.opsForZSet().rangeByScore("endTime", 0, timeInMillis);
        for (Object o : endTime) {
            kafkaTemplate.send("endTime", JSON.toJSONString(o));
            redisTemplate.opsForZSet().remove("endTime",o);
        }
    }
    @Override
    public ResponseResult updateActive(Activity activity) {
        if(activity.getStatus()==2||activity.getStatus()==3){
            return ResponseResult.errorResult(500,"活动正在进行或未开始，不能操作");
        } else{
            activityMapper.updateById(activity);
            return ResponseResult.okResult("修改成功");
        }
    }

    @Override
    public ResponseResult delete(Integer id) {
        Activity activity=activityMapper.selectById(id);
        if(activity.getStatus()==1||activity.getStatus()==2){
            return ResponseResult.errorResult(500,"活动正在进行或未开始，不能操作");
        } else{
            activityMapper.deleteById(id);
            return ResponseResult.okResult("删除成功");
        }
    }
}




